package com.ld.admin.controller;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.model.system.SysMenuModel;
import com.ld.admin.plugin.cache.MenuCacheUtil;
import com.ld.admin.service.DaliySentenceService;
import com.ld.shieldsb.annotation.NoLog;
import com.ld.shieldsb.common.core.model.PropertiesModel;
import com.ld.shieldsb.common.core.model.Result;

import io.swagger.annotations.ApiOperation;

@Controller(value = "adminIndexController")
@RequestMapping("${shieldsb.adminPath}")
public class IndexController extends AdminBasicController<Object> {
    @Autowired
    public DaliySentenceService daliySentenceService;

    // 首页，匹配多url
    @ApiOperation("首页")
    @GetMapping({ "", "/", "/index" })
    public String index() {
        // 读取上传相关的配置文件
        String uploadBaseUrl = "uploadBaseUrl"; // 上传路径的基本url
        String uploadImageHost = "uploadImageHost"; // 图片上传后的显示地址
        String uploadCallbackURL = "uploadCallbackURL"; // 回调url方法
        String uploadGroup = "uploadGroup"; // 组，即项目前缀
        request.setAttribute(uploadBaseUrl, PropertiesModel.CONFIG.getString(uploadBaseUrl)); // 上传路径
        request.setAttribute(uploadImageHost, PropertiesModel.CONFIG.getString(uploadImageHost)); // 图片上传后的显示地址
        request.setAttribute(uploadCallbackURL, PropertiesModel.CONFIG.getString(uploadCallbackURL)); // 回调url方法
        request.setAttribute("uploadGroup", PropertiesModel.CONFIG.getString(uploadGroup)); // 组，即项目前缀

        request.setAttribute("shieldsb_debug", PropertiesModel.CONFIG.getBoolean("shieldsb_debug", false)); // 是否调试模式
        // 覆盖上下文中同名变量
        ServletContext context = request.getServletContext();
        context.setAttribute(uploadBaseUrl, request.getAttribute(uploadBaseUrl));
        context.setAttribute(uploadImageHost, request.getAttribute(uploadImageHost));
        context.setAttribute(uploadCallbackURL, request.getAttribute(uploadCallbackURL));
        context.setAttribute(uploadGroup, request.getAttribute(uploadGroup));

        List<SysMenuModel> topMenus = MenuCacheUtil.getTopMenus(); // 获取菜单
        request.setAttribute("topMenus", topMenus);
        return "a/index";
    }
    // ================================= 每日一句 begin =================================

    @ApiOperation(value = "每日一句", notes = "提交登录")
    @RequestMapping(path = "/daliySentence", method = RequestMethod.POST)
    @ResponseBody
    @NoLog // 不保存日志
    public Object daliySentence() {
        Result result = daliySentenceService.getDaliySentence();
        return result;
    }

    @ApiOperation(value = "跳转到设置每日一句数据源页面", notes = "")
    @RequestMapping(path = "/toSetDSSource", method = RequestMethod.GET)
    public String toSetDaliySentenceSource() {
        daliySentenceService.getAllKeys(request);
        return "a/dSSourceSet";
    }

    @ApiOperation(value = "设置每日一句数据源", notes = "提交登录")
    @RequestMapping(path = "/setDSSource", method = RequestMethod.POST)
    @ResponseBody
    public Object setDaliySentenceSource() {
        List<String> keys = getList("key");
        return daliySentenceService.setDaliySentenceSource(keys, getInt("enable") == 1 ? true : false);
    }
    // ================================= 每日一句 end =================================
}
